//获取所有的表单内容对象
var name_info = document.getElementById('name');
var password_info = document.getElementById('password');
var password_confirm_info = document.getElementById('password_confirm');
var email_info = document.getElementById('email');
var phone_info = document.getElementById('phone_number')
var name_p = document.getElementById('name_p');
var password_p = document.getElementById('password_p');
var password_confirm_p = document.getElementById('password_confirm_p');
var email_p = document.getElementById('email_p');
var phone_p = document.getElementById('phone_number_p')

//获取焦点和失去焦点 每次触发验证一次
for(var i=0,j=document.getElementsByTagName("input");i<j.length;i++){
    j[i].onblur=function(){//表单失去焦点时，显示验证结果
        validateName();
        validatePassword();
        validateConPassword();
        validateEmail();
        validatePhone();
    }
}
//验证账号
var validateName = function() {
    if (name_info.value == "") { //验证是否为空
        name_p.innerHTML = "必填，长度为4~16个字符";
        name_p.style.color = "#BBB";
        name_info.style.border = "1px solid #888";
        return false;
    } else if (name_info.value.length < 4 || name_info.value.length > 16) { //验证长度是否符合标准
        name_p.innerHTML = "长度只能为为4~16个字符";
        name_p.style.color = "#f00";
        name_info.style.border = "2px solid #f00";
        return false;
    } else {
        name_p.innerHTML = "名称格式正确";
        name_p.style.color = "#BBB";
        name_info.style.border = "2px solid #0f0";
        return true;
    }
}
//验证密码
var validatePassword = function(){
    //初步认证
    if(password_info.value.length>3&&password_info.value.length<17){
        password_p.innerHTML = "密码可用";
        password_p.style.color = "#bbb";
        password_info.style.border = "2px solid #0f0"
        return true;
    }
    else if (password_info.value == ""){
        password_p.innerHTML = "请输入密码";
        password_p.style.color = "#BBBBBB";
        password_info.style.border = "1px solid #888";
        return false;
    }
    else{
        //alert(password_info);
        password_p.innerHTML = "密码格式不正确";
        password_p.style.color = "#f00";
        password_info.style.border = "2px solid #888";
        return false;
    }
}
var validateConPassword = function(){
    if(password_confirm_info.value!=""){
        if(password_info.value === password_confirm_info.value){
            password_confirm_p.innerHTML = "密码输入一致";
            password_confirm_p.style.color = "#BBB";
            password_confirm_info.style.border = "2px solid #0f0";
            return true;
        }else{
            password_confirm_p.innerHTML = "密码输入不一致";
            password_confirm_p.style.color = "#f00";
            password_confirm_info.style.border = "2px solid #f00";
            return false;
        }
    }else{
        password_confirm_p.innerHTML = "再次输入相同的密码";
        password_confirm_p.style.color = "#bbb";
        password_confirm_info.style.border = "1px solid #888";
        return false;
    }
}
//验证邮箱
var validateEmail = function(){
    if(email_info.value!=""){
        var filter = /^[a-z0-9]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/;
        if(filter.test(email_info.value)){
            email_p.innerHTML = "邮箱格式正确";
            email_p.style.color = "#BBB";
            email_info.style.border = "2px solid #0f0";
            return true;
        }else{
            email_p.innerHTML = "邮箱格式错误";
            email_p.style.color = "#f00";
            email_info.style.border = "2px solid #f00";
            return false;
        }
    }else{
        email_p.innerHTML = "输入您的邮箱";
        email_p.style.color = "#BBB";
        email_info.style.border = "1px solid #888";
        return false;
    }
}
//验证手机号码
var validatePhone = function(){
    if(phone_info.value != ""){
        var filter = /^1\d{10}$/;
        if(filter.test(phone_info.value)){
            phone_p.innerHTML = "手机格式正确";
            phone_p.style.color = "#BBB";
            phone_info.style.border = "2px solid #0f0";
            return true;
        }else{
            phone_p.innerHTML = "手机格式错误";
            phone_p.style.color = "#f00";
            phone_info.style.border = "2px solid #f00";
            return false;
        }
    }else{
        phone_p.innerHTML = "输入您的手机号码";
        phone_p.style.color = "#BBB";
        phone_info.style.border = "1px solid #888";
        return false;
    }
}

// 在上交前验证所有部分
var validateAll = function (e){
    if(validateName()&&validatePassword()&&validateEmail()&&validatePhone()){
        ;
    }else{
        alert("提交失败，请正确填写。");
        e.preventDefault();
        return false;
    }
}
subObj=document.getElementById("submit");
if(subObj.addEventListener){
    subObj.addEventListener("click",validateAll,false)
}else{
    subObj.attachEvent("onclick",validateAll)
}
